package cn.uc.db;

import java.util.ArrayList;

import cn.uc.bs.DiaryObejct;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class DiaryDatabase{
	private Context mContext = null;
	private DiaryDBHelper mHelper = null;
	private SQLiteDatabase mDb = null;
	private String iDBName;
	
//	private final static String DATABASE_NAME = "weediary.db";
//	private final static String TABLE_ACCOUNT = "account";
//	private final static String TABLE_ID = "_id";
////	private final static String ACCOUNT_ID = "accountID";
//	private final static String TABLE_USERNAME = "username";
//	private final static String TABLE_PSWD = "password";
//	
//	private final static String CREATE_ACCOUNT_TABLE = "CREATE TABLE "+ TABLE_ACCOUNT
//	                      +" (" + TABLE_ID + "INTEGER PRIMARY KEY," + TABLE_USERNAME
//	                      + "TEXT," + TABLE_PSWD + "TEXT)";
//	
	private final static String CREATE_DIARY_TABLE = "CREATE TABLE diary (did varchar(64) PRIMARY KEY,title TEXT,date_time INTEGER,content TEXT)";
	private static final int DB_VERSION = 1;
	
	private static class DiaryDBHelper extends SQLiteOpenHelper
	{

		public DiaryDBHelper(Context context, String aDBName) {
			super(context, aDBName, null, DB_VERSION);
			// TODO Auto-generated constructor stub
		}

		//@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			db.execSQL(CREATE_DIARY_TABLE);
		}

		//@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			db.execSQL("DROP TABLE IF EXISTS diary");
			onCreate(db);
		}
		
	}
	
	public DiaryDatabase(Context context, String aDBName){
	    mContext = context;
	    iDBName = aDBName;
	    mHelper = new DiaryDBHelper(mContext, iDBName);
	}

	public DiaryDatabase(String aDBName){
	    iDBName = aDBName;
	    mHelper = new DiaryDBHelper(mContext, iDBName);
	}
	
	public void openDatabase()throws SQLException
	{
		mHelper = new DiaryDBHelper(mContext, iDBName);
		mDb = mHelper.getWritableDatabase();	
	}

	public void closeDatabase()
	{
		mHelper.close();
	}
	
	public void insertDiary(DiaryObejct aDiaryObejct){
	    SQLiteDatabase sDb = mHelper.getWritableDatabase();
	    //	    String sql = "INSERT INTO diary VALUES(" +
	    //	    		aDiaryObejct.getId() +",'" + aDiaryObejct.getTitle() + "'," + aDiaryObejct.getTimeMills() + ",'" + aDiaryObejct.getContent() + "')";
	    //	    sDb.execSQL(sql);
	    
//	    sDb.execSQL("insert into diary(did,title,date_time,content) values(?,?)", new Object[]{
//	            aDiaryObejct.getId(), aDiaryObejct.getTitle(), aDiaryObejct.getTimeMills(), aDiaryObejct.getContent()});

	    ContentValues iniValues = new ContentValues();
	    iniValues.put("did",  aDiaryObejct.getId());
	    iniValues.put("title", aDiaryObejct.getTitle());
	    iniValues.put("date_time", aDiaryObejct.getTimeMills());
	    iniValues.put("content", aDiaryObejct.getContent());
	    sDb.insert("diary", "did", iniValues);
	    
	    mHelper.close();
	}
	
	public ArrayList<DiaryObejct> getAllDiaries(){
	    SQLiteDatabase sDb = mHelper.getWritableDatabase();
	    ArrayList<DiaryObejct> sObjects = new ArrayList<DiaryObejct>();
	    Cursor cursor= sDb.rawQuery("select * from diary ", null);
//	    Cursor cursor=  sDb.rawQuery("select * from diary limit ?,?",
//	                new String[]{String.valueOf(0),String.valueOf(1)});

	    while(cursor.moveToNext()){
	        DiaryObejct aObejct = new DiaryObejct();	       
	        aObejct.setId(cursor.getString(0));
	        aObejct.setTitle(cursor.getString(1));
	        aObejct.setTimeMills(cursor.getLong(2));
	        aObejct.setContent(cursor.getString(3));
	        sObjects.add(aObejct);
	    }

	    mHelper.close();

	    return sObjects;
	}

//	public long insertAccount(Account account)
//	{
//		ContentValues iniValues = new ContentValues();
//		iniValues.put(ACCOUNT_ID,  account.getId());
//		iniValues.put(TABLE_USERNAME, account.getUserName());
//		iniValues.put(TABLE_PSWD, account.getPswd());
//		
//		return mDb.insert(TABLE_ACCOUNT, TABLE_ID, iniValues);
//	}
	
	public void insertDBObject(DBObject aDBObject){
	    openDatabase();
	    mDb.insert(iDBName, aDBObject.getClass().getName(), aDBObject.dbValues());
	    closeDatabase();
	}
	
	public void deleteDBObject(DBObject aDbObject){
	    openDatabase();
	    mDb.delete(iDBName, aDbObject.getPrimaryKey(), null);
	    closeDatabase();
	}
	
	public ArrayList<DBObject> getAllObjects(DBObjectFactory aFactory){
	    openDatabase();
	    ArrayList<DBObject> sObjects = new ArrayList<DBObject>();
	    Cursor cursor= mDb.rawQuery("select * from " + aFactory.table(), null);
	    
        while(cursor.moveToNext()){
            sObjects.add(aFactory.createDbObject(cursor));
        }
        
        closeDatabase();
	    
	    return sObjects;
	}

	public ArrayList<DBObject> getAllObjects(DBObjectFactory aFactory, int startResult, int maxResult){
	    openDatabase();
	    ArrayList<DBObject> sObjects = new ArrayList<DBObject>();
	    Cursor cursor= mDb.rawQuery("select * from " + aFactory.table() + " limit ?,?",
                new String[]{String.valueOf(startResult),String.valueOf(maxResult)});
	    
        while(cursor.moveToNext()){
            sObjects.add(aFactory.createDbObject(cursor));
        }
        closeDatabase();
	    return sObjects;
	}
	
//	public boolean deleteAccount(String account)
//	{
//	
//		return mDb.delete(TABLE_ACCOUNT, ACCOUNT_ID, null) > 0;
//	}
}
